package ru.belokuriha_online.resort_hotels_belokuriha;


import android.content.Context;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;



public class PicTextAdapter extends BaseAdapter {
	private Context mContext;
	private List<? extends Map<String, ?>> mData;	
	Bitmap appdri;
	int sizes;
	int msizes;
	String picurl;
	Bitmap bmSourse;
	Bitmap bmThumbnail;
		
	public PicTextAdapter(Context c, List<? extends Map<String, ?>> data,int sizes) {
		mContext = c;
		mData = data;
		msizes = sizes;
	}

	public int getCount() {
		return mData.size();
	}

	public Object getItem(int position) {
		return mData.get(position);
	}

	public long getItemId(int position) {
		return position;
	}



	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		View grid;

		if (convertView == null) {
			grid = new View(mContext);
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
			grid = inflater.inflate(R.layout.grid_item, parent, false);
		} else {
			grid = (View) convertView;
		}


		ImageView imageView = (ImageView) grid.findViewById(R.id.imageitemgrid);
		TextView textView = (TextView) grid.findViewById(R.id.textitemgrid);

		
		try {
			picurl = mData.get(position).get("gric").toString();
			if (picurl == null) {
				picurl = "";
			}
			if (picurl.equals("")) {
				bmSourse = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.resort);
				bmThumbnail = ThumbnailUtils.extractThumbnail(bmSourse,
						msizes, msizes);
				imageView.setImageBitmap(bmThumbnail);

				
			} 
			else
			{
				
				File aboutpic = new File(mData.get(position).get("gri").toString());
				InputStream isan = new FileInputStream(aboutpic);
				bmSourse =  BitmapFactory.decodeStream(isan);
				bmThumbnail = ThumbnailUtils.extractThumbnail(bmSourse,
						msizes, msizes);
				imageView.setImageBitmap(bmThumbnail);
			}
			
		}
		catch (Exception e) {
			
			bmSourse = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.resort);
			bmThumbnail = ThumbnailUtils.extractThumbnail(bmSourse,
					msizes, msizes);
			imageView.setImageBitmap(bmThumbnail);

		}

		try {
			
			textView.setText(mData.get(position).get("grt").toString());
			
		}
		catch (Exception e) {
			textView.setText("");
		}
		return grid;
	}





}

